package com.example.jl.bethinner.view;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;

import com.example.jl.bethinner.R;

public class RadarView extends FrameLayout {

    private int viewSize = 500;
    private Paint mPaintSector;
    private Paint mPaintCenterCircle;//中心圆
    private boolean isStart = false;
    private ScanThread mThread;
    private int startAngle = 0;//角度
    private boolean isFailed = false;
    /**
     * 圆环半径 Note: (Re)在onMeasure计算{@link #onMeasure(int, int)}
     */
    private RectF mBounds;
    private final int centerLineWidth = 10;
    private Shader mShader = new SweepGradient(viewSize >> 1, viewSize >> 1, Color.TRANSPARENT, Color.WHITE);//扫描渲染
    private Matrix matrix = new Matrix();

    public RadarView(Context context) {
        super(context);
        init();
    }

    public RadarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        initPaint();



        setBackgroundColor(Color.TRANSPARENT);
        setBackground(getResources().getDrawable(R.mipmap.circle_blue));
    }

    private void initPaint() {
        mPaintCenterCircle = new Paint();
        mPaintCenterCircle.setColor(Color.WHITE);
        mPaintCenterCircle.setStyle(Style.FILL);

        mPaintSector = new Paint();
        mPaintSector.setColor(0x9D00ff00);
        mPaintSector.setAntiAlias(true);
    }

    public void setViewSize(int size) {
        this.viewSize = size;
        mShader = new SweepGradient(viewSize >> 1, viewSize >> 1, Color.TRANSPARENT, Color.WHITE);
        setMeasuredDimension(viewSize, viewSize);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(viewSize, viewSize);
        mBounds = new RectF(getLeft(), getTop(), getRight(), getBottom());
    }

    public boolean isFailed() {
        return isFailed;
    }

    public void setIsFailed(boolean isFailed) {
        this.isFailed = isFailed;
    }

    public void scandFailed() {
        setIsFailed(true);
        //结束线程
        setMThreadToNull();
        if (isStart) {
            isStart = false;
        } else {
            invalidate();
        }

    }

    public void start() {
        //重新扫描,支持扫描失败的情况
        setIsFailed(false);
        startAngle = 0;

        if (!isStart || isFailed){
            //重置线程
            setMThreadToNull();
            mThread = new ScanThread();
            mThread.start();
        }

        isStart = true;
    }
    public void setMThreadToNull(){
        if (null != mThread) {
            mThread.interrupt();
            mThread = null;
        }
    }
    public void stopOrRestart() {
        //当扫描中断之后继续，不支持扫描失败的情况下重新扫描
        if (isFailed)
            return;
        if (isStart) {
            isStart = false;
            //结束线程1
            mThread.interrupt();
            mThread = null;

        } else {
            isStart = true;
            mThread = new ScanThread();
            mThread.start();
        }
    }

    public Boolean isStart() {
        return isStart;
    }

    public void setStart(Boolean start) {
        this.isStart = start;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isFailed) {
            setBackground(getResources().getDrawable(R.mipmap.circle_mark));
        } else {
            setBackground(getResources().getDrawable(R.mipmap.circle_blue));
            int halfRadarSize = viewSize >> 1;

            mPaintSector.setShader(mShader);
            canvas.concat(matrix);
            canvas.drawCircle(halfRadarSize, halfRadarSize, halfRadarSize, mPaintSector);//画渐变圆
            canvas.drawCircle(halfRadarSize, halfRadarSize, (halfRadarSize) >> 4, mPaintCenterCircle);//画中心圆
            //因为考虑SweepGradient的原因，这条线要水平向右
            //复用画圆画笔，设置一下直线的粗细。
            mPaintCenterCircle.setStrokeWidth(centerLineWidth);
            canvas.drawLine(halfRadarSize, halfRadarSize, viewSize, halfRadarSize, mPaintCenterCircle);//画中心线
        }

    }

    protected class ScanThread extends Thread {

        int halfRadaSize = viewSize >> 1;

        @Override
        public void run() {
            Log.d("Out,isStart", "" + isStart);
            while (isStart && !isFailed) {
                startAngle = startAngle + 2;
                matrix.reset();
                matrix.postRotate(startAngle, halfRadaSize, halfRadaSize);
                postInvalidate();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}